package m.member.manage.action;

import m.common.action.ActionMeta;
import m.common.action.VuePageResult;
import m.common.action.VueResult;
import m.html.VueFormResult;
import m.html.element.Element;
import m.html.form.*;
import m.member.manage.model.AppSetting;
import m.member.manage.service.AppSettingService;
import m.system.cache.CacheUtil;
import m.system.util.JSONMessage;
import manage.action.ManageAction;

@ActionMeta(name = "member/manage/appSetting")
public class AppSettingAction extends ManageAction {
    private AppSetting model;
    public JSONMessage doSave() throws Exception {
        verifyAdminOperPower("app_setting_power");
        getService(AppSettingService.class).save(model);
        return new JSONMessage(0)
                .push("msg","修改成功");
    }
    public VueResult toEdit() throws Exception {
        verifyAdminOperPower("app_setting_power");
        ModelForm form=new ModelForm("会员日设置");
        form.addItem(
            FormInput.radio("类型","model.memberDayType","m.member.manage.model.AppSetting!memberDayType").setSpan(3),
            FormTest.test("model.memberDayType=='N'",
                FormHtml.ins("", Element.tag("a-alert").A("message","当前设置无会员日")).setSpan(3)
            ).other(
                FormTest.test("model.memberDayType=='A'",FormInput.checkBoxGroup("每月","model.memberDayNo","m.member.manage.model.AppSetting!memberDayNo").setSpan(3)),
                FormTest.test("model.memberDayType=='B'",FormInput.radio("每周","model.memberWeekNo","m.member.manage.model.AppSetting!memberWeekNo").setSpan(3)),
                FormInput.number("积分倍数","model.pointRate","%").setPlaceholder("请输入积分倍数"),
                FormHtml.ins("","会员日当天积分翻倍（范围可设置100~500），例如：消费66.6元，非会员日积分<span class=\"text-red\">+66</span>，会员日翻倍后<span class=\"text-red text-lg\">+{{formatNumber(66*model.pointRate/100,\"0\")}}</span>。").setSpan(2),
                FormInput.number("优惠比例","model.discount","%").setPlaceholder("请输入优惠比例"),
                FormHtml.ins("","会员日当天下单额外优惠（范围可设置0~100），例如：订单最终金额88.8元，会员日只需<span class=\"text-red text-lg\">{{formatNumber(88.8*(100-model.discount)/100,\"0.00\")}}元</span>。").setSpan(2)
            )
        )
        .addData("model",CacheUtil.get(AppSetting.class,""))
        .addButton(
            FormButton.pri("保存","doSave")
        )
        .addMethod(
            FormEvent.backFlag,
            FormEvent.verify("verifyModel",
                FormJsTest.falseRemind("this.model.pointRate&&parseInt(this.model.pointRate)>=100&&parseInt(this.model.pointRate)<=500","积分倍数范围错误"),
                FormJsTest.falseRemind("this.model.discount&&parseInt(this.model.discount)>=0&&parseInt(this.model.discount)<=100","优惠比例范围错误")
            ),
            FormEvent.doJson("doSave",actionUrl("doSave"),"{model:this.model}")
                .before("verifyModel").success("backFlag")
        );
        VueFormResult result=new VueFormResult();
        return result.addForm(form);
//        以上写法可根据系统设置统一样式，下面写法是前端页面写法
//        return new VuePageResult("page/member/manage/appSetting.js")
//                .addParam("model", CacheUtil.get(AppSetting.class,""));
    }

    public AppSetting getModel() {
        return model;
    }

    public void setModel(AppSetting model) {
        this.model = model;
    }
}
